unit FTPService;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, TCPService,
  ContextFactory, FTPDirectory, MemBuffer, FTPClientContext;

type
  TFTPService = class(TTCPService)
  public
    constructor Create(AOwner: TComponent); override;
    procedure CloseASocket(FChannel: TObject); override;
    procedure NewSocket(FChannel: TObject); override;
    procedure RecvData(FDIChannel: TObject; FDIBuffer: TIBuffer;
      dwIoSize: Cardinal); override;
    procedure SendData(FDIChannel: TObject; FDIBuffer: TIBuffer;
      dwIoSize: Cardinal); override;
    function OnCreateClient: TIClientContext; override;
    procedure AppendDisplayMsg(pFormat: PChar; const Args: array of const);
    procedure AppendLogMessage(pFormat: PChar; const Args: array of const);
    procedure AppendErrorLogMessage(pFormat: PChar; const Args: array of const);
  end;

  procedure Register;

implementation

uses FTPEngine, TCPListener;

procedure Register;
begin
  RegisterComponents('TIOCP', [TFTPService]);
end;

{ TFTPService }


constructor TFTPService.Create(AOwner: TComponent);
begin
  inherited;
  Port := 8988;
  MaxMemPool := 2000;
  MaxClientPool := 1000;
  MaxWorkPool := 4;
  _TCPEngine := TIFTPEngine.Create;
  _TCPEngine.OnChannelOpenEvent := NewSocket;
  _TCPEngine.OnChannelCloseEvent := CloseASocket;
  _TCPEngine.OnChannelRecvEvent := RecvData;
  _TCPEngine.OnChannelSendEvent := SendData;
  _TCPListener := TITCPListener.Create;
  _TCPListener.FOnCreateClientEvent := OnCreateClient;
end;

procedure TFTPService.NewSocket(FChannel: TObject);
var
  sIP: string;
begin
  sIP := (FChannel as TIClientContext).RemoteIP;
  if Assigned(OpenSocket) then OpenSocket(FChannel);
end;

procedure TFTPService.CloseASocket(FChannel: TObject);
var
  sIP: string;
begin
  sIP := (FChannel as TIClientContext).RemoteIP;
  if Assigned(FChannel) then CloseSocket(FChannel);
end;

procedure TFTPService.AppendDisplayMsg(pFormat: PChar;
  const Args: array of const);
begin

end;

procedure TFTPService.AppendErrorLogMessage(pFormat: PChar;
  const Args: array of const);
begin

end;

procedure TFTPService.AppendLogMessage(pFormat: PChar;
  const Args: array of const);
begin

end;

function TFTPService.OnCreateClient: TIClientContext;
begin
  Result := TFTPClientContext.Create(_TCPEngine);
end;

procedure TFTPService.RecvData(FDIChannel: TObject; FDIBuffer: TIBuffer;
  dwIoSize: Cardinal);
begin
  if Assigned(RecvSocket) then RecvSocket(FDIChannel, FDIBuffer, dwIoSize);
end;

procedure TFTPService.SendData(FDIChannel: TObject; FDIBuffer: TIBuffer;
  dwIoSize: Cardinal);
begin
  if Assigned(SendSocket) then SendSocket(FDIChannel, FDIBuffer, dwIoSize);
end;

end.
